//
//  EDSCommentInputView.m
//  DriveSchoolCoach
//
//  Created by TangBin on 2019/8/9.
//  Copyright © 2019 TangBin. All rights reserved.
//

#import "EDSCommentInputView.h"
#import "TYZPlaceholderTextView.h"


#define MIN_TEXTVIEW_HEIGHT 34
#define MAX_TEXTVIEW_HEIGHT 60

@interface EDSCommentInputView () <UITextViewDelegate>

@property (nonatomic, strong) CALayer *topLine;

@property (nonatomic, strong) UIButton *btnSend; ///< 发送按钮

@property (nonatomic, assign) CGFloat defaultHeight; ///< 默认高度

@property (nonatomic, strong) UIView *contentBgView; ///< 输入框的背景

@property (nonatomic, strong) TYZPlaceholderTextView *textView;


- (void)initWithSubView;

- (void)initWithBtnSend;

- (void)initWithContentBgView;

- (void)initWithTextView;

@end

@implementation EDSCommentInputView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self initWithSubView];
    }
    return self;
}

- (void)initWithSubView
{
    self.defaultHeight = self.height - IPHONEX_BOTTOM_SPACE;
    self.inputViewHeight = self.defaultHeight;
    self.backgroundColor = [UIColor whiteColor];
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOffset = CGSizeMake(0, -1);
    self.layer.shadowOpacity = 0.1;
    self.layer.shadowRadius = 4;
//    CGRect frame = CGRectMake(0, 0, kScreenWidth, 1);
//    _topLine = [CALayer drawLine:self frame:frame lineColor:[UIColor colorWithHexString:@"#e3e3e3"]];
    
    [self initWithBtnSend];
    
    [self initWithContentBgView];
    
    [self initWithTextView];
}

- (void)initWithBtnSend
{
    CGRect frame = CGRectMake(kScreenWidth - 30 - 30 - 15, (kEDSCommentInputViewHeight - IPHONEX_BOTTOM_SPACE - 34) * 0.5, 30 + 30, 34);
    _btnSend = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnSend.frame = frame;
    [_btnSend setTitle:@"发表" forState:UIControlStateNormal];
    [_btnSend setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:UIControlStateNormal];
    _btnSend.titleLabel.font = FONTSIZE(14);
    _btnSend.backgroundColor = kThemeColor;
    _btnSend.layer.cornerRadius = frame.size.height * 0.5;
    _btnSend.layer.masksToBounds = YES;
    [_btnSend addTarget:self action:@selector(actionSend:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_btnSend];
}

- (void)initWithContentBgView
{
    CGRect frame = CGRectMake(15, _btnSend.top, _btnSend.left - 10 - 15, 34);
    _contentBgView = [[UIView alloc] initWithFrame:frame];
    _contentBgView.layer.cornerRadius = frame.size.height * 0.5;
    _contentBgView.layer.masksToBounds = YES;
    _contentBgView.layer.borderColor =[UIColor colorWithHexString:@"#e3e3e3"].CGColor;
    _contentBgView.layer.borderWidth = 1;
    _contentBgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_contentBgView];
}

- (void)initWithTextView
{
    if (!_textView)
    {
        CGRect frame = CGRectMake(5, 0, _contentBgView.width - 10, MIN_TEXTVIEW_HEIGHT);
        _textView = [[TYZPlaceholderTextView alloc] initWithFrame:frame];
        _textView.backgroundColor = [UIColor whiteColor];
        _textView.returnKeyType = UIReturnKeySend;
        _textView.delegate = self;
        _textView.placeholder = @"来说两句评论一下吧~";//@"发表您的看法...";
        _textView.placeHolderLabel.top = 0;
        _textView.placeHolderLabel.height = MIN_TEXTVIEW_HEIGHT;
        _textView.showsVerticalScrollIndicator = NO;
        _textView.font = FONTSIZE(14);
        [_contentBgView addSubview:_textView];
    }
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    _textView.placeholder = placeholder;
    _textView.placeHolderLabel.top = 0;
    _textView.placeHolderLabel.height = MIN_TEXTVIEW_HEIGHT;
}

- (void)resetHeight
{
    self.textView.text = nil;
    self.height = kEDSCommentInputViewHeight;
    self.inputViewHeight = self.height;
    _contentBgView.height = 34;
    _textView.height = 34;
    _btnSend.centerY = self.contentBgView.centerY;//(kEDSCommentInputViewHeight - IPHONEX_BOTTOM_SPACE - 34);
}

- (void)actionSend:(id)sender
{
    if (self.sendCommentContentBlock)
    {
        self.sendCommentContentBlock(_textView.text);
    }
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
    CGFloat width = CGRectGetWidth(textView.frame);
//    debugLog(@"width=%f", width);
    CGSize newSize = [textView sizeThatFits:CGSizeMake(width, MAXFLOAT)];
//    debugLogSize(newSize);
    CGFloat newHeight = newSize.height;
    if (newSize.height < MIN_TEXTVIEW_HEIGHT)
    {
        newHeight = MIN_TEXTVIEW_HEIGHT;
    }
    if (newSize.height >= MAX_TEXTVIEW_HEIGHT)
    {
        newHeight = MAX_TEXTVIEW_HEIGHT;
    }
//    debugLog(@"heie=%f", newHeight + (49 - 34));
    self.height = newHeight + (49 - 34);
    self.bottom = self.keyboardY;
//    debugLog(@"keyboardY=%f", self.keyboardY);
    
    self.inputViewHeight = self.height;
    self.contentBgView.height = newHeight;
    self.textView.height = newHeight;
    self.btnSend.centerY = self.contentBgView.centerY;
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        // 返回NO，表示键盘失效
        [self actionSend:nil];
        return NO;
    }
    return YES;
}

//- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
//{
//
//}
//- (BOOL)textViewShouldEndEditing:(UITextView *)textView
//{
//
//}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    debugLog(@"textViewDidBeginEditing");
    if (self.touchTextEditBlock)
    {
        self.touchTextEditBlock(YES);
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    debugLog(@"textViewDidEndEditing");
    if (self.touchTextEditBlock)
    {
        self.touchTextEditBlock(NO);
    }
}

@end
